//
//  Color++.swift
//  chat
//
//  Created by 1 on 2023/4/12.
//

import SwiftUI

//extension Color {
//    static func rgb(_ red: CGFloat, green: CGFloat, blue: CGFloat) -> Color {
//        return Color(red: red / 255, green: green / 255, blue: blue / 255)
//    }
//
//    static func Hex(_ hex: UInt) -> Color {
//        let r: CGFloat = CGFloat((hex & 0xFF0000) >> 16)
//        let g: CGFloat = CGFloat((hex & 0x00FF00) >> 8)
//        let b: CGFloat = CGFloat(hex & 0x0000FF)
//        return rgb(r, green: g, blue: b)
//    }
//}

extension Color {
    init(hex: String, opacity: Double = 1.0) {
        
        var hexString = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

        if hexString.hasPrefix("#") {
            hexString.remove(at: hexString.startIndex)
        }
        
        let scanner = Scanner(string: hexString)
        var rgbValue: UInt64 = 0

        scanner.scanHexInt64(&rgbValue)

        let r = Double((rgbValue & 0xff0000) >> 16) / 255.0
        let g = Double((rgbValue & 0xff00) >> 8) / 255.0
        let b = Double(rgbValue & 0xff) / 255.0

        self.init(red: r, green: g, blue: b, opacity: opacity)
    }
    
    init(hex: String) {
        self.init(hex: hex,opacity: 1)
    }
}

